package org.shunya.duplicatefileperquisitor.gui;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.shunya.duplicatefileperquisitor.model.FileTypeExtensions;
import org.shunya.duplicatefileperquisitor.model.FileTypes;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import static org.shunya.duplicatefileperquisitor.model.FileTypes.*;

public class FileTypesDialogController extends AnchorPane implements Initializable {
    @FXML Button addMaskBtn;
    @FXML Button editBtn;
    @FXML Button deleteBtn;
    @FXML Button restoreDefaultsBtn;
    @FXML Button okBtn;
    @FXML TableView<FileTypeExtensions> fileTypesTable;
    @FXML TableColumn checkBoxColumn;
    @FXML TableColumn fileTypesColumn;
    private DuplicateFilePerquisitorMain application;
    private Stage stage;
    private TextField fileTypesTxt;

    public void onAddMaskBtnActionEvent() {
        try {
            FileTypeExtensions fileTypeExtensions = new FileTypeExtensions("", "");
            application.gotoFileMaskAndGetFileTypeExtensions(fileTypeExtensions);
            fileTypesTable.getItems().add(fileTypeExtensions);
        } catch (Exception e) {

        }
    }

    public void onOkBtnActionEvent() {
        String extensions = "";
        for (FileTypeExtensions fileTypeExtensions : fileTypesTable.getItems()) {
            if(fileTypeExtensions.isSelected())
            extensions += fileTypeExtensions.getExtensions() + ";";
        }
        fileTypesTxt.setText(extensions);
        stage.close();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        initTable();
    }

    private void initTable() {
        checkBoxColumn.setCellValueFactory(new PropertyValueFactory<FileTypes, Boolean>("selected"));
        checkBoxColumn.setCellFactory(new Callback<TableColumn<FileTypes, Boolean>, TableCell<FileTypes, Boolean>>() {
            public TableCell<FileTypes, Boolean> call(TableColumn<FileTypes, Boolean> p) {
                CheckBoxTableCell<FileTypes, Boolean> cell = new CheckBoxTableCell<>();
                cell.setAlignment(Pos.CENTER);
                return cell;
            }
        });
        fileTypesColumn.setCellValueFactory(new PropertyValueFactory<FileTypes, String>("fileTypesWithExtensions"));
        fileTypesColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        List<FileTypeExtensions> fileTypeExtensions = new ArrayList<>();
        fileTypeExtensions.add(new FileTypeExtensions(AnyFile.getFileTypeDescription(), "*.*"));
        fileTypeExtensions.add(new FileTypeExtensions(AdobeAcrobatFiles.getFileTypeDescription(), "*.PDF"));
        fileTypeExtensions.add(new FileTypeExtensions(ArchiveFiles.getFileTypeDescription(), "*.7Z;*.ACE;*.ALZ;*.ARC;*.ARJ;*.BZ2;*.BZ2;*.CAB;*.JAR;*.LZH;*.PAK;*.RAR;*.AT3;*.TGZ;*.TAR;*.GZ;*.UHA;*.ZIP"));
        fileTypeExtensions.add(new FileTypeExtensions(MSOfficeFiles.getFileTypeDescription(), "*.MDB;*.ADP;*.MDA;*.MDE;*.ADE;*.XL;*.XLA;*.XLS*;*.XLT;*.XLM;*.XLC;*.XLW;*.PPS;*.PPT*;*.DOC*;*.WRI;*.CSV"));
        fileTypeExtensions.add(new FileTypeExtensions(OpenOfficeFiles.getFileTypeDescription(), "*.ODT;*.ODS;*.ODG;*.ODP;*.ODM;*.ODF"));
        fileTypeExtensions.add(new FileTypeExtensions(MusicFiles.getFileTypeDescription(), "*.M4A;*.M4P;*.MP2;*.AAC;*.AIFF;*.APE;*.AU;*.FLAC;*.MID;*.MP3;*.OGG;*.RAW;*.WAV;*.WMA;*.MP3PRO;*.RM;*.RA"));
        fileTypeExtensions.add(new FileTypeExtensions(VideoFiles.getFileTypeDescription(), "*.3GP;*.MP4;*.SWF;*.FLA;*.FLV;*.MKV;*.MPEG;*.MPG;*.M1V;*.MP2;*.MPA;*.MPE;*.MOV;*.AVI;*.WMV;*.DIVX"));
        fileTypeExtensions.add(new FileTypeExtensions(PhotoImages.getFileTypeDescription(), "*.BMP;*.GIF;*.ICO;*.JNG;*.JPG;*.JPEG;*.JPE;*.JP2;*.JPC;*.J2K;*.PNG;*.TIF;*.TIFF;*.EPS;*.CLP;*.CRW;*.CR2;*.DCX;*.EXR;*.KDC;*.PCX;*.PSD;*.RAW;*.SFW;*.WMF"));
        fileTypeExtensions.add(new FileTypeExtensions(TextFiles.getFileTypeDescription(), "*.TXT;*.RTF"));
        fileTypeExtensions.add(new FileTypeExtensions(WebFiles.getFileTypeDescription(), "*.ASP;*.CSS;*.HTML;*.HTM;*.PHP;*.XHTML;*.JS"));
        fileTypesTable.setItems(FXCollections.observableList(fileTypeExtensions));
        fileTypesTable.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                // Get the table header
                Pane header = (Pane)fileTypesTable.lookup("TableHeaderRow");
                if(header!=null && header.isVisible()) {
                    header.setMaxHeight(0);
                    header.setMinHeight(0);
                    header.setPrefHeight(0);
                    header.setVisible(false);
                    header.setManaged(false);
                }
            }
        });
    }


    public void setApplication(DuplicateFilePerquisitorMain application) {
        this.application = application;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public void setFileTypesTxt(TextField fileTypesTxt) {
        this.fileTypesTxt = fileTypesTxt;
    }
}
